package com.jyy.prefabricated.controller;


import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.entity.Student;
import com.jyy.prefabricated.entity.StudentQuestionCollection;
import com.jyy.prefabricated.service.StudentQuestionCollectionService;
import com.jyy.prefabricated.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 学员题库收藏
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class StudentQuestionCollectionController {

    @Autowired
    private UserUtils userUtils;

    @Autowired
    private StudentQuestionCollectionService studentQuestionCollectionService;

    /**
     * 收藏题库
     * @param studentQuestionCollection
     * @return
     */
    @PostMapping("/student-question-collection")
    public AjaxResult insert(@Valid @RequestBody StudentQuestionCollection studentQuestionCollection){
        log.info("Rest request to insert studentQuestionCollection:{}",studentQuestionCollection);
        Student student = userUtils.getCurrentStudent();
        studentQuestionCollection.setStudentId(student.getId());
        Integer id = studentQuestionCollectionService.insert(studentQuestionCollection);
        StudentQuestionCollection result = studentQuestionCollectionService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新收藏题库
     * @param studentQuestionCollection
     * @return
     */
    @PutMapping("/student-question-collection")
    public AjaxResult update(@Valid @RequestBody StudentQuestionCollection studentQuestionCollection){
        log.info("Rest request to update studentQuestionCollection:{}",studentQuestionCollection);
        studentQuestionCollectionService.update(studentQuestionCollection);
        StudentQuestionCollection result = studentQuestionCollectionService.selectById(studentQuestionCollection.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询收藏题库详情
     * @param id
     * @return
     */
    @GetMapping("/student-question-collection/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find studentQuestionCollection by id:{}",id);
        StudentQuestionCollection result = studentQuestionCollectionService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除收藏题库
     * @param questionBankId
     * @return
     */
    @DeleteMapping("/student-question-collection/{questionBankId}")
    public AjaxResult deleteById(@PathVariable Integer questionBankId){
        Student student = userUtils.getCurrentStudent();
        log.info("Rest request to delete studentQuestionCollection by questionBankId:{},studentId{}",questionBankId,student.getId());
        studentQuestionCollectionService.deleteByQuestionBankIdAndStudentId(questionBankId,student.getId());
        return AjaxResult.success();
    }


}
